﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Configuration;

namespace WebSys.UI.Control.ZhengShou
{
    public class EditProjectControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_PROJECT";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["pid"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
                QSDataDLL();
                clickData();
                GetLXH(1);
            }            
            Persistence1.BindControl = panel1;
            BindData();
            DropDownList ddl区属 = (DropDownList)FindControl("ddl区属");
            ddl区属.SelectedIndexChanged += new EventHandler(ddl区属_SelectedIndexChanged);
            IsLookPerson();
            Label showhxmsg = (Label)FindControl("showhxmsg");
            string jsstrIP = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
            if (jsstrIP == ConfigurationManager.AppSettings["zsgis"].ToString())
            {

            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "displaytr", "<script>displaytr();</script>");
            }
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '"+userid+"')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count >0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btnSave = (Button)FindControl("btnSave");
                        btnSave.Visible = false;
                    }
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            //GetLXH(1);
        }

        #region 返回立项号
        /// <summary>
        /// 返回立项号
        /// </summary>
        /// <param name="type">1：初始加载立项号 0：改变立项号</param>
        public void GetLXH(int type)
        {
            //string pid = HttpContext.Current.Request["pid"];
            //if (!string.IsNullOrEmpty(pid))
            //{
                TextBox txtlxh = (TextBox)FindControl("txt立项号");
                DropDownList ddl区属 = (DropDownList)FindControl("ddl区属");

                if (type==1)
                {
                    if (string.IsNullOrEmpty(txtlxh.Text.Trim()))
                    {
                        string sql = @"select max(sxh) from (select (case
       when length(立项号)=9 then substr(立项号,7,3)
       else '0'
 end) sxh,(case
       when length(立项号)=9 then substr(立项号,1,4)
       else '0'
 end) yy  from ZS_PROJECT where 区属='" + ddl区属.SelectedValue.Trim() + @"') where yy=to_char(sysdate,'yyyy')";
                        string lxh = _EluZSDataProvider.GetString(sql);
                        if (string.IsNullOrEmpty(lxh))
                        {
                            lxh = "001";
                        }
                        else
                        {
                            lxh = Convert.ToString(Convert.ToInt32(lxh) + 1);
                        }
                        if (lxh.Length == 1)
                        {
                            lxh = "00" + lxh;
                        }
                        if (lxh.Length == 2)
                        {
                            lxh = "0" + lxh;
                        }
                        //string qsv = _EluZSDataProvider.GetSomeWhere("zs_params", "keyvalue", "  name='区属' and keyname='" + ddl区属.SelectedValue + "'");
                        //if (qsv.Length < 10)
                        //{
                        //    qsv = "0" + qsv;
                        //}
                        string qsv = GetQWDM();
                        if (!string.IsNullOrEmpty(qsv))
                        {
                            txtlxh.Text = DateTime.Now.Year.ToString() + qsv + lxh;
                        }
                        else
                        {
                            txtlxh.Text = "";
                        }
                        
                    }
                }
                else
                {
                    string sql = @"select max(sxh) from (select (case
       when length(立项号)=9 then substr(立项号,7,3)
       else '0'
 end) sxh,(case
       when length(立项号)=9 then substr(立项号,1,4)
       else '0'
 end) yy  from ZS_PROJECT where 区属='" + ddl区属.SelectedValue.Trim() + @"') where yy=to_char(sysdate,'yyyy')";
                    string lxh = _EluZSDataProvider.GetString(sql);
                    if (string.IsNullOrEmpty(lxh))
                    {
                        lxh = "001";
                    }
                    else
                    {
                        lxh = Convert.ToString(Convert.ToInt32(lxh) + 1);
                    }
                    if (lxh.Length == 1)
                    {
                        lxh = "00" + lxh;
                    }
                    if (lxh.Length == 2)
                    {
                        lxh = "0" + lxh;
                    }
                    //string qsv = _EluZSDataProvider.GetSomeWhere("zs_params", "keyvalue", "  name='区属' and keyname='" + ddl区属.SelectedValue + "'");
                    //if (qsv.Length < 10)
                    //{
                    //    qsv = "0" + qsv;
                    //}
                    //txtlxh.Text = DateTime.Now.Year.ToString() + qsv + lxh;
                    string qsv = GetQWDM();
                    if (!string.IsNullOrEmpty(qsv))
                    {
                        txtlxh.Text = DateTime.Now.Year.ToString() + qsv + lxh;
                    }
                    else
                    {
                        txtlxh.Text = "";
                    }
                }
               
            //}
        }
        #endregion

        #region 获得区位代码
        /// <summary>
        /// 获得区位代码
        /// </summary>
        /// <returns></returns>
        public string GetQWDM()
        {
            string qwdm = string.Empty;
            DropDownList ddl区属 = (DropDownList)FindControl("ddl区属");
            string sql = "select keyvalue from zs_params where name='区位代码' and keyname='"+ddl区属.SelectedItem.Text.Trim()+"'";
             qwdm = _EluZSDataProvider.GetString(sql);
            return qwdm;
        }
        #endregion

        #region 绑定
        protected void BindData() {
            string pid = HttpContext.Current.Request["pid"];
            
            
            Button btnUpfile = (Button)FindControl("btnUpfile");
            Button btnSave = (Button)FindControl("btnSave");
            Button btnBack = (Button)FindControl("btnBack");
            LinkButton LinkButton1 = (LinkButton)FindControl("LinkButton1");

            btnUpfile.Click += new EventHandler(btnUpfile_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnBack.Click += new EventHandler(btnBack_Click);
            LinkButton1.Click += new EventHandler(LinkButton1_Click);

            Label Label2 = (Label)FindControl("Label2");
            Label Label3 = (Label)FindControl("Label3");
            Label Label4 = (Label)FindControl("Label4");

            if (_EluZSDataProvider.GetProjectFJNo(pid, "项目批准文件","1") == 1)
            {
                Label2.Visible = false;
            }
            if (_EluZSDataProvider.GetProjectFJNo(pid, "规划许可文件","1") == 1)
            {
                Label3.Visible = false;
            }
            if (_EluZSDataProvider.GetProjectFJNo(pid, "土地权属证明文件","1") == 1)
            {
                Label4.Visible = false;
            }

            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                LinkButton1.Visible = false;
            }
        }
        #endregion

        #region 历史记录链接
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ZhengSFWLog.aspx?type=0&pid=" + HttpContext.Current.Request["PID"]);
        }
        #endregion

        #region 保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            TextBox lxh = (TextBox)FindControl("txt立项号");
            string lxhstr = lxh.Text.Trim(); 
            if (lxhstr.Length==9)
            {
                string qs = lxhstr.Substring(4, 2);
            
                string qsv = GetQWDM();
                if (!string.IsNullOrEmpty(qsv))
                {
                    if (qs!=qsv)
                    {
                        base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('立项号的区位代码和区属不一致')</script>");
                        return;
                    }
                }
            }
           
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            TextBox txtID = (TextBox)FindControl("txtID");
            string url = "EditProject.aspx?PID=" + Persistence1.KeyValue;
            base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功');location.href='"+url+"'</script>");
           
        }
        #endregion

        #region 返回
        protected void btnBack_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ProjectList.aspx");
        }
        #endregion


        protected void btnUpfile_Click(object sender, EventArgs e)
        {
            string pID = (HttpContext.Current.Request["PID"] == null ? "" : HttpContext.Current.Request["PID"].ToString());
            EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(pID);
            if (project.提交状态 == "1")
            {
                HttpContext.Current.Response.Redirect("FileUpload.aspx?PID=" + HttpContext.Current.Request["PID"] + "&flag=show");
            }
            else
            {
                HttpContext.Current.Response.Redirect("FileUpload.aspx?PID=" + HttpContext.Current.Request["PID"] + "");
            }
        }
        private void clickData()
        {
            Label Label5 = (Label)FindControl("Label5");
            Label Label7 = (Label)FindControl("Label7");
            Label Label6 = (Label)FindControl("Label6");
            Label 提示 = (Label)FindControl("提示");
            Label5.Visible = false;
            Label6.Visible = false;
            string strPID = HttpContext.Current.Request["PID"];
            int tt = _EluZSDataProvider.GetProjectFJAllNo(strPID,"1");
            if (tt < 3 && tt > 0)
            {
                StringBuilder mybuilder = _EluZSDataProvider.GetProFJName(strPID,"1");
                Label5.Visible = true;
                Label6.Visible = true;
                提示.Text = mybuilder.ToString();
                Label7.Visible = false;
            }
            else if (tt == 0)
            {
                Label5.Visible = false;
                Label6.Visible = false;
                提示.Visible = false;
                Label7.Visible = true;
            }
            else
            {
                Label5.Visible = false;
                Label6.Visible = false;
                提示.Visible = false;
                Label7.Visible = false;
            }
            
        }
        protected void QSDataDLL()
        {
            DropDownList ddl区属 = (DropDownList)FindControl("ddl区属");
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("区属");
            //_params.Insert(0, null);
            ddl区属.DataSource = _params;
            ddl区属.DataValueField = "keyname";
            ddl区属.DataTextField = "keyname";
            ddl区属.DataBind();
        } 

        public void ddl区属_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextBox txt征收实施单位 = (TextBox)FindControl("txt征收实施单位");
            TextBox txt征收部门 = (TextBox)FindControl("txt征收部门");
            DropDownList ddl区属 = (DropDownList)FindControl("ddl区属");
            string qs = ddl区属.SelectedValue;
            if (!string.IsNullOrEmpty(qs))
            {
                txt征收部门.Text = _EluZSDataProvider.GetString("select 单位名称 from zs_company where 单位类别='征收部门' and 单位名称 like '%" + qs + "%'");
                txt征收实施单位.Text = _EluZSDataProvider.GetString("select 单位名称 from zs_company where 单位类别='征收实施单位' and 所属区属='" + qs + "'");
               //如果是编辑如果区属改变了，立项号就改变
                string pid = HttpContext.Current.Request["pid"];
                if (!string.IsNullOrEmpty(pid))
                {
                    string sql = "select 区属 from zs_project where id='" + pid + "'";
                    string oldqs = _EluZSDataProvider.GetString(sql);
                    if (oldqs != qs)
                    {
                        GetLXH(0);
                    }
                    else
                    {
                        TextBox txtlxh = (TextBox)FindControl("txt立项号");
                        string lx = _EluZSDataProvider.GetString("select 立项号 from zs_project where id='" + pid + "' ");
                        if (string.IsNullOrEmpty(lx))
                        {
                            GetLXH(0);
                        }
                        else
                        {
                            txtlxh.Text = lx;
                        }
                    }
                }
                else//重新生成立项号
                {
                    GetLXH(0);
                }
            }
            

        }
    }
}


